/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.SetuBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.SetuBillDao;
import com.minierp.dao.SetuBillItemDao;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncSetuBillFormCtrHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.CodeRule;
import com.minierp.model.Establishment;
import com.minierp.model.SetuBill;
import com.minierp.model.TallySet;
import com.minierp.service.SetuBillService;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class SetuBillFormCtr extends SimpleFormController {
	private SetuBillItemDao setuBillItemDao;
	private SetuBillDao setuBillDao;
	private BusinessBillItemDao businessBillItemDao;
	private MakeTypeDao makeTypeDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;
	private SetuBillService setuBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("SETU_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for setu bill");
		} else {
			SetuBillBean form;
			BusinessBill businessBill;
			List makeTypeList;
			if (id != null && !id.equals("")) {
				SetuBill bbId1 = (SetuBill) this.setuBillDao.get(
						SetuBill.class, Long.parseLong(id));
				if (bbId1 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				businessBill = (BusinessBill) this.setuBillDao.get(
						BusinessBill.class, bbId1.getBbId());
				if (businessBill == null) {
					throw new Exception("Business bill not found for id:"
							+ bbId1.getBbId());
				}

				TallySet est1 = (TallySet) this.setuBillDao.get(TallySet.class,
						bbId1.getTsId());
				Establishment bbItemList2 = (Establishment) this.setuBillDao
						.get(Establishment.class, businessBill.getClientId());
				makeTypeList = this.setuBillItemDao.getListBySetuBill(bbId1
						.getId());
				List bbItemList1 = this.businessBillItemDao
						.getList(businessBill.getId());
				List makeTypeList1 = this.makeTypeDao.getList(false);
				form = EncSetuBillFormCtrHelper.doFormBackingObjectEdit(cr,
						bbId1, makeTypeList, bbItemList2,
						businessBill.getBillCode(), bbItemList1, makeTypeList1,
						est1);
			} else {
				String bbId = request.getParameter("bb_id");
				businessBill = (BusinessBill) this.setuBillDao.get(
						BusinessBill.class, Long.parseLong(bbId));
				if (businessBill == null) {
					throw new Exception("Business bill not found for id:"
							+ bbId);
				}

				Establishment est = (Establishment) this.setuBillDao.get(
						Establishment.class, businessBill.getClientId());
				if (est == null) {
					throw new Exception("Can\'t find client for id:"
							+ businessBill.getClientId());
				}

				List bbItemList = this.businessBillItemDao
						.getListWidthExt(businessBill.getId());
				makeTypeList = this.makeTypeDao.getList(false);
				form = EncSetuBillFormCtrHelper.doFormBackingObjectNew(request,
						cr, est, businessBill, bbItemList, makeTypeList);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		SetuBillBean form = (SetuBillBean) command;
		EncSetuBillFormCtrHelper.doOnBind(form, request, this.dataSource);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		SetuBillBean form = (SetuBillBean) command;
		SetuBill obj = form.getSetuBill();
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "SETU_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExam("Y");
			obj.setExamId(ub.getStaffer().getId());
			obj.setExamName(ub.getStaffer().getStafferName());
		} else {
			obj.setExam("N");
		}

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("exam_img");
		if (multipartFile != null && !multipartFile.isEmpty()) {
			String w = multipartFile.getOriginalFilename();
			String extendName = EncCommonHelper.getExtendName(w);
			String uploadPath = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			String realPath = this.getServletContext().getRealPath(uploadPath);
			String fileName = ub.getUser().getId() + "_"
					+ System.currentTimeMillis() + "." + extendName;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + fileName));
			obj.setExamImg(uploadPath + fileName);
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "SETU_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.setuBillService.txUpdate(form, ub, this.setuBillDao,
						this.setuBillItemDao, this.businessBillItemDao,
						this.idGenerator);
			} catch (CannotAcquireLockException arg17) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg18) {
				EncCommonHelper.setAlertMessage(request, arg18.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		} else {
			try {
				this.setuBillService.txInsert(form, ub, this.setuBillDao,
						this.setuBillItemDao, this.businessBillItemDao,
						this.serialNumberDao, this.idGenerator);
			} catch (CannotAcquireLockException arg19) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg20) {
				EncCommonHelper.setAlertMessage(request, arg20.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		return mv;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SetuBillDao getSetuBillDao() {
		return this.setuBillDao;
	}

	public void setSetuBillDao(SetuBillDao setuBillDao) {
		this.setuBillDao = setuBillDao;
	}

	public SetuBillItemDao getSetuBillItemDao() {
		return this.setuBillItemDao;
	}

	public void setSetuBillItemDao(SetuBillItemDao setuBillItemDao) {
		this.setuBillItemDao = setuBillItemDao;
	}

	public BusinessBillItemDao getBusinessBillItemDao() {
		return this.businessBillItemDao;
	}

	public void setBusinessBillItemDao(BusinessBillItemDao businessBillItemDao) {
		this.businessBillItemDao = businessBillItemDao;
	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public SetuBillService getSetuBillService() {
		return this.setuBillService;
	}

	public void setSetuBillService(SetuBillService setuBillService) {
		this.setuBillService = setuBillService;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
}